﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Collections.ObjectModel;
using Microsoft.Xna.Framework.Media;
using SMARTMobile.Common;
using SMARTMobile.Models.Local;
using SMARTMobile.Models.UI;
using SMARTMobile.ViewModels;
using System.Windows.Media;
using C1.Phone.FlexGrid;
using System.Collections;
using SMARTMobile.WinPhone;
using Telerik.Windows.Controls;
using SmartMobile.Data;
using SMARTMobile.Models;

namespace SMARTMobile.Views
{
    public partial class NhapDuLieuPage
    {
        private string currentCell;
        private bool isxplainOutDoor;
        private bool isxplainInDoor;
        private bool isMerchandising; 
        public NhapDuLieuPage()
        {
            InitializeComponent();
           
           
            //var col = this.OutletGrid.Columns["Province"];
            //col.Visible = false;
            //var col = this.OutletGrid.Columns["ProvinceCode"];

            //col.ValueConverter = new ColumnValueConverter((this.DataContext as NhapDuLieuViewModel).PrivinceList);

            //Load Gender
            //var col = this.ContactInforGrid.Columns["Gender"];
            //List<string> genderDictionary = new List<string>();
            //genderDictionary.Add("Male");
            //genderDictionary.Add("Female");
            //col.ValueConverter = new ColumnValueConverter(genderDictionary, false);

            //var col = this.CurrentOutDoorDisplayGrid.Columns["PhoneVendorCode"];
            //col.ValueConverter = new ColumnValueConverter((this.DataContext as NhapDuLieuViewModel).PhoneVenderList);

        }

        private void ExitButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/MainPage.xaml", UriKind.Relative));
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            //string msg;
            //if (NavigationContext.QueryString.TryGetValue("msg", out msg))
            //{
            //    if (!string.IsNullOrEmpty(msg) && msg.Contains("ExplainOutDoor"))
            //    {
            //        isxplainOutDoor = true;
            //        string tempName = msg.Split(';')[1];
            //        currentCell = tempName;
                   
            //        this.CurrentOutEx.IsExpanded = true;
            //    }

            //    if (!string.IsNullOrEmpty(msg) && msg.Contains("ExplainInDoor"))
            //    {
            //        isxplainInDoor = true;
            //        string tempName = msg.Split(';')[1];
            //        currentCell = tempName;

            //        this.CurrentInEx.IsExpanded = true;
            //    }

            //    if (!string.IsNullOrEmpty(msg) && msg.Contains("ExplainMerchandising"))
            //    {
            //        isMerchandising = true;
            //        string tempName = msg.Split(';')[1];
            //        currentCell = tempName;

            //        this.OutletNokiaMercEx.IsExpanded = true;
            //    }
            //}
        }

        private void AddRowButton_OnClick(object sender, RoutedEventArgs e)
        {
            ////IGridViewColumn column1 = new GridViewTextColumn();
            ////column1.Header = "CustomerKey";
            ////column1.Width = 150;
            ////OutletGridView.Columns.Add(column1);

            ////PriceRangeContribution item = new PriceRangeContribution { CustomerKey = " 1", ContributionByValue = " 2", ContributionByVolume = " 3", PriceRangeCode = " " };
            ////model.PriceRangeContribution.Add(item);
            ////OutletGridView.ItemsSource = model.PriceRangeContribution;
        }

        //private void PhonevendorEx_ExpandedStateChanged(object sender, Telerik.Windows.Controls.ExpandedStateChangedEventArgs e)
        //{
        //    if (this.PhonevendorEx.IsExpanded)
        //    {
        //        this.PriceRangeEx.IsExpanded = false;
        //        this.OutletPGEx.IsExpanded = false;
        //        this.OutletVenderEx.IsExpanded = false;
        //        this.ContactEx.IsExpanded = false;
        //        this.CurrentOutEx.IsExpanded = false;
        //        this.CurrentInEx.IsExpanded = false;
        //        this.OutletNokiaEx.IsExpanded = false;
        //        this.OutletNokiaMercEx.IsExpanded = false;
        //    }
        //}

        //private void PriceRangeEx_ExpandedStateChanged(object sender, Telerik.Windows.Controls.ExpandedStateChangedEventArgs e)
        //{
        //    if (this.PriceRangeEx.IsExpanded)
        //    {
        //        this.PhonevendorEx.IsExpanded = false;
        //        this.OutletPGEx.IsExpanded = false;
        //        this.OutletVenderEx.IsExpanded = false;
        //        this.ContactEx.IsExpanded = false;
        //        this.CurrentOutEx.IsExpanded = false;
        //        this.CurrentInEx.IsExpanded = false;
        //        this.OutletNokiaEx.IsExpanded = false;
        //        this.OutletNokiaMercEx.IsExpanded = false;
        //    }
        //}

        //private void OutletVenderEx_ExpandedStateChanged(object sender, Telerik.Windows.Controls.ExpandedStateChangedEventArgs e)
        //{
        //    if (this.OutletVenderEx.IsExpanded)
        //    {
        //        this.PriceRangeEx.IsExpanded = false;
        //        this.PhonevendorEx.IsExpanded = false;
        //        this.OutletPGEx.IsExpanded = false;
        //        this.ContactEx.IsExpanded = false;
        //        this.CurrentOutEx.IsExpanded = false;
        //        this.CurrentInEx.IsExpanded = false;
        //        this.OutletNokiaEx.IsExpanded = false;
        //        this.OutletNokiaMercEx.IsExpanded = false;
        //    }
        //}

        //private void ContactEx_ExpandedStateChanged(object sender, Telerik.Windows.Controls.ExpandedStateChangedEventArgs e)
        //{
        //    if (this.ContactEx.IsExpanded)
        //    {
        //        this.PriceRangeEx.IsExpanded = false;
        //        this.PhonevendorEx.IsExpanded = false;
        //        this.OutletPGEx.IsExpanded = false;
        //        this.OutletVenderEx.IsExpanded = false;
        //        this.CurrentOutEx.IsExpanded = false;
        //        this.CurrentInEx.IsExpanded = false;
        //        this.OutletNokiaEx.IsExpanded = false;
        //        this.OutletNokiaMercEx.IsExpanded = false;
        //    }
        //}

        //private void CurrentOutEx_ExpandedStateChanged(object sender, Telerik.Windows.Controls.ExpandedStateChangedEventArgs e)
        //{
        //    OpenCurrentOutEx();
        //}

        //public void OpenCurrentOutEx()
        //{
        //    if (this.CurrentOutEx.IsExpanded)
        //    {
        //        this.PriceRangeEx.IsExpanded = false;
        //        this.PhonevendorEx.IsExpanded = false;
        //        this.OutletPGEx.IsExpanded = false;
        //        this.OutletVenderEx.IsExpanded = false;
        //        this.ContactEx.IsExpanded = false;
        //        this.CurrentInEx.IsExpanded = false;
        //        this.OutletNokiaEx.IsExpanded = false;
        //        this.OutletNokiaMercEx.IsExpanded = false;

        //        if (!string.IsNullOrEmpty(currentCell))
        //        {
        //            (this.DataContext as NhapDuLieuViewModel).ImageName = currentCell;
        //        }

        //        var col = this.CurrentOutDoorDisplayGrid.Columns["OutdoorTypeCode"];
        //        col.ValueConverter = new ColumnValueConverter((this.DataContext as NhapDuLieuViewModel).OutDoorTypeList);
        //    }
        //}

        //private void CurrentInEx_ExpandedStateChanged(object sender, Telerik.Windows.Controls.ExpandedStateChangedEventArgs e)
        //{
        //    if (this.CurrentInEx.IsExpanded)
        //    {
        //        this.PriceRangeEx.IsExpanded = false;
        //        this.PhonevendorEx.IsExpanded = false;
        //        this.OutletPGEx.IsExpanded = false;
        //        this.OutletVenderEx.IsExpanded = false;
        //        this.ContactEx.IsExpanded = false;
        //        this.CurrentOutEx.IsExpanded = false;
        //        this.OutletNokiaEx.IsExpanded = false;
        //        this.OutletNokiaMercEx.IsExpanded = false;

        //        var col = this.CurrentInDoorDisplayGrid.Columns["IndoorTypeCode"];
        //        col.ValueConverter = new ColumnValueConverter((this.DataContext as NhapDuLieuViewModel).InDoorTypeList);
        //    }
        //}

        //private void OutletPGEx_ExpandedStateChanged(object sender, Telerik.Windows.Controls.ExpandedStateChangedEventArgs e)
        //{
        //    if (this.OutletPGEx.IsExpanded)
        //    {
        //        this.PriceRangeEx.IsExpanded = false;
        //        this.PhonevendorEx.IsExpanded = false;
        //        this.OutletVenderEx.IsExpanded = false;
        //        this.ContactEx.IsExpanded = false;
        //        this.CurrentOutEx.IsExpanded = false;
        //        this.CurrentInEx.IsExpanded = false;
        //        this.OutletNokiaEx.IsExpanded = false;
        //        this.OutletNokiaMercEx.IsExpanded = false;
        //    }
        //}

        //private void OutletNokiaEx_ExpandedStateChanged(object sender, Telerik.Windows.Controls.ExpandedStateChangedEventArgs e)
        //{
        //    if (this.OutletNokiaEx.IsExpanded)
        //    {
        //        this.PriceRangeEx.IsExpanded = false;
        //        this.PhonevendorEx.IsExpanded = false;
        //        this.OutletPGEx.IsExpanded = false;
        //        this.OutletVenderEx.IsExpanded = false;
        //        this.ContactEx.IsExpanded = false;
        //        this.CurrentOutEx.IsExpanded = false;
        //        this.CurrentInEx.IsExpanded = false;
        //        this.OutletNokiaMercEx.IsExpanded = false;
        //    }
        //}

        //private void OutletNokiaMercEx_ExpandedStateChanged(object sender, Telerik.Windows.Controls.ExpandedStateChangedEventArgs e)
        //{
        //    if (this.OutletNokiaMercEx.IsExpanded)
        //    {
        //        this.PriceRangeEx.IsExpanded = false;
        //        this.PhonevendorEx.IsExpanded = false;
        //        this.OutletPGEx.IsExpanded = false;
        //        this.OutletVenderEx.IsExpanded = false;
        //        this.ContactEx.IsExpanded = false;
        //        this.CurrentOutEx.IsExpanded = false;
        //        this.CurrentInEx.IsExpanded = false;
        //        this.OutletNokiaEx.IsExpanded = false;
        //    }
        //}
       
        private void CurrentOutDoorDisplayGrid_OnClick(object sender, RoutedEventArgs e)
        {
            //var ht = CurrentOutDoorDisplayGrid.HitTest(e);

            //if (ht.Column == 3)
            //{
            //   var test = CurrentOutDoorDisplayGrid.SelectedItem;
            //    this.CurrentOutEx.IsExpanded = true;
            //    GlobalInformation.StateImageType = 0;
            //    NavigationService.Navigate(new Uri("/Views/ChupHinhCuaHangPage.xaml?msg=" + "NhapDuLieuOut;" + ht.Column.ToString() + ":" + ht.Row.ToString(), UriKind.Relative));
            //}
        }

        public BitmapImage GetLastImage()
        {
            var imageList = new ObviousCollection<Image>();
            using (var library = new MediaLibrary())
            {
                Picture photoFromLibrary = library.Pictures[library.Pictures.Count - 1];
                BitmapImage latestBitmapImage = new BitmapImage();
                latestBitmapImage.SetSource(photoFromLibrary.GetImage());

                return latestBitmapImage;
            }
        }

        private void CurrentOutDoorDisplayGrid_OnLoadedRows(object sender, EventArgs e)
        {
            //if (!string.IsNullOrEmpty(currentCell) && isxplainOutDoor)
            //{
            //    int row = int.Parse(currentCell.Split(':')[1]);
            //    CurrentOutDoorDisplayGrid.SelectedIndex = row;

            //    if (CurrentOutDoorDisplayGrid.SelectedItem != null)
            //    {
            //        var currentOutDoor = (CurrentOutDoorDisplayGrid.SelectedItem as CurrentOutDoorDisplayExtend);
            //        string imageName = "NhapDuLieu\\" + "OUT" + currentOutDoor.Customerkey + currentOutDoor.PhoneVendorCode.Trim() + currentOutDoor.OutDoorTypeCode.Trim() + ".jpeg";
            //        Utilities.ImageUtilities.SaveToJpeg(GetLastImage(), imageName, "NhapDuLieu");
            //        (CurrentOutDoorDisplayGrid.SelectedItem as CurrentOutDoorDisplayExtend).Picture = "OUT" + currentOutDoor.PhoneVendorCode.Trim() + currentOutDoor.OutDoorTypeCode.Trim() + ".jpeg";
            //    }
            //}
        }

        private void CurrentInDoorDisplayGrid_OnClick(object sender, RoutedEventArgs e)
        {
            //var ht = CurrentInDoorDisplayGrid.HitTest(e);

            //if (ht.Column == 3)
            //{
            //    var test = CurrentInDoorDisplayGrid.SelectedItem;
            //    this.CurrentInEx.IsExpanded = true;
            //    GlobalInformation.StateImageType = 0;
            //    NavigationService.Navigate(new Uri("/Views/ChupHinhCuaHangPage.xaml?msg=" + "NhapDuLieuIn;" + ht.Column.ToString() + ":" + ht.Row.ToString(), UriKind.Relative));
            //}
        }

        private void CurrentInDoorDisplayGrid_OnLoadedRows(object sender, EventArgs e)
        {
            //if (!string.IsNullOrEmpty(currentCell) && isxplainInDoor)
            //{
            //    int row = int.Parse(currentCell.Split(':')[1]);
            //    CurrentInDoorDisplayGrid.SelectedIndex = row;

            //    if (CurrentInDoorDisplayGrid.SelectedItem != null)
            //    {
            //        var currentOutDoor = (CurrentInDoorDisplayGrid.SelectedItem as CurrentInDoorDisplayExtend);
            //        string imageName = "NhapDuLieu\\" + "IN" + currentOutDoor.CustomerKey + currentOutDoor.PhoneVendorCode.Trim() + currentOutDoor.InDoorTypeCode.Trim() + ".jpeg";
            //        Utilities.ImageUtilities.SaveToJpeg(GetLastImage(), imageName, "NhapDuLieu");
            //        (CurrentInDoorDisplayGrid.SelectedItem as CurrentInDoorDisplayExtend).Picture = "IN" + currentOutDoor.PhoneVendorCode.Trim() + currentOutDoor.InDoorTypeCode.Trim() + ".jpeg";
            //    }
            //}
        }

        private void OutletNokiaMerchandisingPotentialGrid_Click(object sender, RoutedEventArgs e)
        {
            //var ht = OutletNokiaMerchandisingPotentialGrid.HitTest(e);

            //if (ht.Column == 2)
            //{
            //    var test = OutletNokiaMerchandisingPotentialGrid.SelectedItem;
            //    this.OutletNokiaMercEx.IsExpanded = true;
            //    GlobalInformation.StateImageType = 0;
            //    NavigationService.Navigate(new Uri("/Views/ChupHinhCuaHangPage.xaml?msg=" + "Merchandising;" + ht.Column.ToString() + ":" + ht.Row.ToString(), UriKind.Relative));
            //}
        }

        private void OutletNokiaMerchandisingPotentialGrid_LoadedRows(object sender, EventArgs e)
        {
            //if (!string.IsNullOrEmpty(currentCell) && isMerchandising)
            //{
            //    int row = int.Parse(currentCell.Split(':')[1]);
            //    OutletNokiaMerchandisingPotentialGrid.SelectedIndex = row;

            //    if (OutletNokiaMerchandisingPotentialGrid.SelectedItem != null)
            //    {
            //        var merchandising = (OutletNokiaMerchandisingPotentialGrid.SelectedItem as OutletNokiaMerchandisingPotentialExtend);
            //        string imageName = "Merchandising\\" + merchandising.Customerkey + merchandising.MerchandisingCode + ".jpeg";
            //        Utilities.ImageUtilities.SaveToJpeg(GetLastImage(), imageName, "Merchandising");
            //        (OutletNokiaMerchandisingPotentialGrid.SelectedItem as OutletNokiaMerchandisingPotentialExtend).Picture = "Mer" + merchandising.MerchandisingCode + ".jpeg";
            //    }
            //}
        }

        private void RadExpanderControl_ExpandedStateChanged(object sender, ExpandedStateChangedEventArgs e)
        {
            OutletAdvanceInforListing selectedAdvance = this.RadListBox.SelectedItem as OutletAdvanceInforListing;
            if (selectedAdvance != null)
            {
                switch (selectedAdvance.InforName.ToString())
                {
                    case "PhonevendorVolume":
                        {
                            List<OutletAdvanceInforTable> advanceInforTableList = (DataContext as NhapDuLieuViewModel).OutletAdvanceInforTableObservable.Where(a => a.DataTable == selectedAdvance.DataTable).OrderBy(a => a.FieldShow).ToList();
                            foreach (var advanceInforTable in advanceInforTableList)
                            {
                                
                            }

                            break;
                        }
                    case "PriceRangeContribution":
                        break;
                    default:
                        break;
                }
                //(DataContext as NhapDuLieuViewModel).BindingFlexgrid(selectedAdvance);              
            }
        }
    }
}